package net.csdn.business.common.utils;

import cn.hutool.core.lang.Validator;
import net.csdn.business.common.enums.ResultCodeEnum;
import net.csdn.business.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collection;

/**
 * @author likun
 * @date 2022/6/14 15:05
 */
public class ValidatorUtil {
    public static ValidatorUtil builder() {
        return new ValidatorUtil();
    }

    public ValidatorUtil checkNull(Object object, String filedName) throws BusinessException {
        if (Validator.isEmpty(object)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, filedName);
        }
        return this;
    }

    /**
     * 判断是null
     *
     * @param object    对象
     * @param filedName 字段名称
     * @return this
     * @throws BusinessException
     */
    public ValidatorUtil checkNull2(Object object, String filedName) throws BusinessException {
        if (Validator.isNull(object)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, filedName);
        }
        return this;
    }

    public ValidatorUtil checkCollection(Collection<?> data, String filedName) {
        if (CollectionUtils.isEmpty(data)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, filedName);
        }
        return this;
    }

    public ValidatorUtil checkSize(Collection<?> data, int size, String filedName) {
        if (CollectionUtils.isEmpty(data)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, filedName);
        }

        if (data.size() > size) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, filedName);
        }
        return this;
    }

    public ValidatorUtil checkSizeAllowEmpty(Collection<?> data, int size, String filedName) {
        if (!CollectionUtils.isEmpty(data) && data.size() > size) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, filedName);
        }
        return this;
    }

    public ValidatorUtil checkString(String object, String filedName) {
        if (StringUtils.isBlank(object)) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, filedName);
        }
        return this;
    }

    public ValidatorUtil checkStringLen(String object, int min, int max, String filedName) {
        this.checkString(object, filedName);
        int len = object.length();
        if (len < min || len > max) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, filedName);
        }
        return this;
    }

    public ValidatorUtil checkInt(Integer object, int max, String filedName) {
        checkInt(object, 0, max, filedName);
        return this;
    }

    public ValidatorUtil checkInt(Integer object, int min, int max, String filedName) {
        checkNull(object, filedName);
        if (object < min || object > max) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, filedName);
        }
        return this;
    }
}